﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web.Mvc;
using KaytechSoftware.Cms.App.Common;

namespace KaytechSoftware.Cms.App.Areas.Admin.Models.Base
{
    public class GridEntityFiledDescriptor
    {
        public RendererType Renderer { get; set; }
        public string LabelResourceKey { get; set; }
        public string Name { get; set; }
        public string TextValue { get; set; }
        public int? IntegerValue { get; set; }
        public decimal? DecimalValue { get; set; }
        public PhotoDescriptor ImageValue { get; set; }
        public IList<SelectListItem> Items { get; set; }

        public long? SelectedItem
        {
            set
            {
                if (Items == null)
                    throw new ApplicationException("Items list cannot be empty.");

                if (!value.HasValue)
                    return;

                var selectedItem = Items.FirstOrDefault(i => i.Value == value.Value.ToString(CultureInfo.InvariantCulture));
                if (selectedItem != null)
                    selectedItem.Selected = true;
            }
        }
    }
}